package com.black.app.service.impl.my;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.my.AppMyData;
import com.black.app.domain.my.AppMyDrinkDetail;
import com.black.app.domain.req.my.mydata.AppMyDataDrinkDetailAddReq;
import com.black.app.service.my.AppMyDrinkDetailService;
import com.black.app.mapper.my.AppMyDrinkDetailMapper;
import com.black.common.exception.ServiceException;
import com.black.common.utils.MessageUtils;
import com.black.common.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author 18494
* @description 针对表【app_my_drink_detail】的数据库操作Service实现
* @createDate 2024-10-23 20:49:29
*/
@Service
public class AppMyDrinkDetailServiceImpl extends ServiceImpl<AppMyDrinkDetailMapper, AppMyDrinkDetail>
    implements AppMyDrinkDetailService{

    @Override
    public boolean addDrink(AppMyDataDrinkDetailAddReq req) {

        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        // 判断日期是否存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppMyDrinkDetail>().eq(AppMyDrinkDetail::getDate, req.getDate()))) {
            throw new ServiceException(MessageUtils.message("app.my.detail.date.exists"));
        }

        AppMyDrinkDetail newEntity = new AppMyDrinkDetail();
        BeanUtils.copyProperties(req, newEntity);
        boolean save = this.save(newEntity);

        return save;
    }

    @Override
    public PageInfo<AppMyDrinkDetail> getDrink(AppMyDataDrinkDetailAddReq req) {
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.getDrink(req);
        });
    }
}




